﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.IO;

namespace mamnontuoitho7.backend
{
    public partial class AlbumPhoto : System.Web.UI.Page
    {
        private Album _album = new Album();
        private ThumbnailClass _thumb = new ThumbnailClass();
        private ImageAlbum _imageAlbum = new ImageAlbum();

        //protected int i;
        protected DataTable _dt;
        protected DataTable _dtImage;
        protected string _productID = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (Request.QueryString["ProductID"] != null)
                    {
                        // Session["AlbumPhoto_productID"] = Request.Form["hidProductID"].ToString();
                        _productID = Request.QueryString["ProductID"].ToString();
                        hid_AlbumID.Value = _productID;
                    }
                    if (Request.Form["hidProductID"] != null)
                    {
                       // Session["AlbumPhoto_productID"] = Request.Form["hidProductID"].ToString();
                        _productID = Request.Form["hidProductID"].ToString();
                        hid_AlbumID.Value = _productID;
                    }
                    if (_productID == "")
                    {
                        _productID = hid_AlbumID.Value;
                    }
                    _dt = _album.GetAlbumByIDAdmin(_productID);
                    if (!IsPostBack)
                    {
                        txtTitleName.Text = _dt.Rows[0]["Title"].ToString();
                        _dtImage = _imageAlbum.GetImageByProductID(_productID);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }

                if (Request.Form["hidImageIDDelete"] != null)
                {
                    try
                    {
                        string _imageID = Request.Form["hidImageIDDelete"].ToString();
                        string _imageName = Request.Form["hidImageNameDelete"].ToString();

                        string FilePath = Request.PhysicalApplicationPath + "images/album/album-" + _productID + "/photo/" + _imageName;
                        string FilePath2 = Request.PhysicalApplicationPath + "images/album/album-" + _productID + "/photo/thumbnail/" + _imageName;

                        _imageAlbum.ImageDelete(_imageID);

                        bool check = _imageAlbum.ImageDelete(_imageID);
                        if (check)
                        {

                            if (File.Exists(FilePath))
                            {
                                File.Delete(FilePath);
                                File.Delete(FilePath2);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorAdminPage);
                    }
                        Response.Redirect("../backend/AlbumPhoto.aspx?ProductID=" + _productID);
                }
                string jscript = "function UploadComplete(){" + ClientScript.GetPostBackEventReference(LinkButton1, "") + "};";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FileCompleteUpload", jscript, true);

                flashUpload.QueryParameters = "ProductID=" + _productID;
                Server.UrlEncode(Request.QueryString.ToString());
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            _dtImage = _imageAlbum.GetImageByProductID(_productID);
        }

        //protected void Uploader_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
        //{
        //    string ext = Path.GetExtension(args.FileName).ToLower().Trim();
        //    string name = args.FileGuid.ToString();
        //    string filename = _productID + "-" + name + ext;

        //    _imageAlbum.ImageInsert(_productID, filename);// tạo record


        //    filename = filename.Length > 70 ? filename.Substring(0, 70) : filename;
        //    string FilePath = Request.PhysicalApplicationPath + "images/album/album-" + _productID + "/photo/" + filename;

        //    ImageMedia img = ImageMedia.Create(File.ReadAllBytes(args.GetTempFilePath()));

        //    float imgWidth = img.Width;
        //    float imgHeight = img.Height;

        //    //////// tạo hình lớn trong thu muc photo; thu nhỏ lại nếu wa lớn với 1canh là 600
        //    int size = 600;
        //    float imgSize1 = imgHeight < imgWidth ? imgHeight : imgWidth;
        //    //the approximately pixel size of thumbnail image is 150 x 150 based on ratio             
        //    float imgResize1 = imgSize1 <= size ? (float)1.0 : size / imgSize1;

        //    float imgWidth1 = imgWidth * imgResize1;
        //    float imgHeight1 = imgHeight * imgResize1;
        //    //generating the thumbnail    

        //    img.Resize((int)imgWidth1, (int)imgHeight1);
        //    File.WriteAllBytes(FilePath, img.ToByteArray());

        //    //// hình nhỏ trong thư mục photo/thumbnail; height luôn là 106
        //    int height = 106;
        //    float imgResize2 = imgHeight <= height ? (float)1.0 : height / imgHeight;
        //    float width = imgWidth * imgResize2;

        //    string FilePath2 = Request.PhysicalApplicationPath + "images/album/album-" + _productID + "/photo/thumbnail/" + filename;

        //    img.Resize((int)width, (int)height);
        //    File.WriteAllBytes(FilePath2, img.ToByteArray());

        //    _dtImage = _imageAlbum.GetImageByProductID(_productID);

        //}



        //protected void Uploader1_UploadCompleted(object sender, CuteWebUI.UploaderEventArgs[] args)
        //{
        //    _productID = hid_AlbumID.Value;
        //    Response.Redirect("../backend/AlbumPhoto.aspx?ProductID="+_productID);
        //}

      
    }
}